package com.zengqingfa.thread.demo4;

/**
 * @fileName: UnsafeBuyTickets
 * @author: zengqf3
 * @date: 2020-10-7 18:22
 * @description:
 */
public class UnsafeBuyTickets {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread t1 = new Thread(ticket, "A");
        Thread t2 = new Thread(ticket, "B");
        Thread t3 = new Thread(ticket, "C");
        t1.start();
        t2.start();
        t3.start();

    }

}

class Ticket implements Runnable {

    private int ticketNums = 10;

    private boolean flag = true;

    @Override
    public void run() {
        //买票
        while (flag) {
            buy();
        }

    }

    private void buy() {
        if (ticketNums <= 0) {
            flag = false;
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "拿到" + ticketNums--);
    }
}